/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-12
*/
package com.rzico.order.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.exception.CustomException;
import com.rzico.order.entity.*;
import com.rzico.order.enumx.ReturnStatusEnum;
import com.rzico.order.enumx.ShippingStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.rzico.order.mapper.ReturnMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 * 退货单项业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class ReturnService extends BaseServiceImpl<Return, String> {

    @Autowired
    private ReturnMapper returnMapper;


    @Autowired
    private ReturnItemService returnItemService;

    @Autowired @Lazy
    private EmptyBarrelService emptyBarrelService;

    @Override
    public BaseMapper<Return, String> getMapper() {
        return returnMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertUseGeneratedKeys(Return returns) {
        returns.setSubCost(returns.calculateSubCost());
        returns.setSubQuantity(returns.calculateSubQuantity());
        returns.setSubPrice(returns.calculateSubPrice());
        int affectCount = super.insertUseGeneratedKeys(returns);
        if (affectCount > 0){
            for (ReturnItem returnItem : returns.getItemList()){
                returnItem.setReturnId(returns.getId());
            }
            returnItemService.insertUseGeneratedKeyList(returns.getItemList());
        }
        return affectCount;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(Object[] ids) {
        int rw = 0;
        for (Object id:ids) {
            Return returns = selectByPrimaryKey(id);
            returns.setDeleted(true);
            rw = rw + super.updateByPrimaryKeySelective(returns);
        }
        return rw;
    }

    @Transactional(rollbackFor = Exception.class)
    public int received(String id) {

        Return returns = selectByPrimaryKey(id);
        if (null == returns){
            throw new CustomException("未查找到配送单");
        }
        if (!returns.getShippingStatus().equals(ReturnStatusEnum.RETURN_STATUS_UNCONFIRM.getId())){
            throw new CustomException("订单已确认");
        }
        if (returns.getShippingStatus().equals(ReturnStatusEnum.RETURN_STATUS_CANCELED.getId())){
            throw new CustomException("订单已关闭");
        }

        returns.setDeliveryDate(new Date());
        returns.setShippingStatus(ReturnStatusEnum.RETURN_STATUS_DISPATCH.getId());
        Integer w = super.updateByPrimaryKeySelective(returns);

        //送达时，登记出桶程回桶

        emptyBarrelService.addReturn(returns);

        return w;

    }


    @Transactional(rollbackFor = Exception.class)
    public int complete(Long id){
        Return returns = selectByPrimaryKey(id);
        if (null == returns){
            throw new CustomException("未查找到退货单");
        }
//        if (returns.getShippingStatus().equals(ReturnStatusEnum.RETURN_STATUS_UNCONFIRM.getId()) ){
//            throw new CustomException("没有确认不能,无法完成");
//        }
        returns.setShippingStatus(ReturnStatusEnum.RETURN_STATUS_COMPLETE.getId());
        returns.setCompleteDate(new Date());
        int affectCount = super.updateByPrimaryKeySelective(returns);

        emptyBarrelService.addReturn(returns);

        return affectCount;
    }

    @Transactional(rollbackFor = Exception.class)
    public int cancel(Long id){
        Return returns = selectByPrimaryKey(id);
        if (null == returns){
            throw new CustomException("未查找到配送单");
        }
        if (!returns.getShippingStatus().equals(ReturnStatusEnum.RETURN_STATUS_UNCONFIRM.getId()) ){
            throw new CustomException("不能取消");
        }
        returns.setShippingStatus(ReturnStatusEnum.RETURN_STATUS_CANCELED.getId());
        return super.updateByPrimaryKeySelective(returns);
    }

    public int cancelAskfor(Long askforId) {
        Map<String,Object> params = new HashMap<>();
        params.put("askforId",askforId);
        return returnMapper.cancelAskfor(params);
    }

}
